package manage.student.view;

import manage.entity.StudentDo;
import manage.handler.UpdateStudentViewHandler;
import manage.service.StudentService;
import manage.service.impl.StudentServiceImpl;
import manage.student.MainView;

import javax.swing.*;
import java.awt.*;

public class UpdateStudentView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel idLabel=new JLabel("编号",JLabel.RIGHT);
    JTextField idText=new JTextField();
    JLabel nameLabel=new JLabel("姓名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel sexLabel=new JLabel("性别",JLabel.RIGHT);
    JTextField sexText=new JTextField();
    JLabel gradeLabel=new JLabel("班级",JLabel.RIGHT);
    JTextField gradeText=new JTextField();
    JLabel ChineseLabel=new JLabel("语文成绩",JLabel.RIGHT);
    JTextField ChineseText=new JTextField();
    JLabel MathLabel=new JLabel("数学成绩",JLabel.RIGHT);
    JTextField MathText=new JTextField();
    JLabel EnglishLabel=new JLabel("英语成绩",JLabel.RIGHT);
    JTextField EnglishText=new JTextField();
    JButton updateBtn=new JButton("修改");

    UpdateStudentViewHandler updateStudentViewHandler;



    public UpdateStudentView(MainView mainView, int selectedStudentId){
        super(mainView,"修改学生",true);
        updateStudentViewHandler=new UpdateStudentViewHandler(this,mainView);
        //查询selectedStudentIds对应的记录并回显
        StudentService studentService=new StudentServiceImpl();
        StudentDo selectedStu=studentService.getById(selectedStudentId);



        //添加组件
        idLabel.setPreferredSize(new Dimension(80,30));
       idText.setPreferredSize(new Dimension(200,30));
        jPanel.add(idLabel);
        idText.setText(selectedStu.getId()+"");
        //设置Id不可编辑
        idText.setEnabled(false);
        jPanel.add(idText);

        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        nameText.setText(selectedStu.getName());
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        sexLabel.setPreferredSize(new Dimension(80,30));
        sexText.setPreferredSize(new Dimension(200,30));
        sexText.setText(selectedStu.getSex());
        jPanel.add(sexLabel);
        jPanel.add(sexText);

        gradeLabel.setPreferredSize(new Dimension(80,30));
        gradeText.setPreferredSize(new Dimension(200,30));
        gradeText.setText(selectedStu.getGrade());
        jPanel.add(gradeLabel);
        jPanel.add(gradeText);

        ChineseLabel.setPreferredSize(new Dimension(80,30));
        ChineseText.setPreferredSize(new Dimension(200,30));
        ChineseText.setText(String.valueOf(selectedStu.getChinese()));
        jPanel.add(ChineseLabel);
        jPanel.add(ChineseText);

        MathLabel.setPreferredSize(new Dimension(80,30));
        MathText.setPreferredSize(new Dimension(200,30));
        MathText.setText(String.valueOf(selectedStu.getMath()));
        jPanel.add(MathLabel);
        jPanel.add(MathText);

        EnglishLabel.setPreferredSize(new Dimension(80,30));
        EnglishText.setPreferredSize(new Dimension(200,30));
        EnglishText.setText(String.valueOf(selectedStu.getEnglish()));
        jPanel.add(EnglishLabel);
        jPanel.add(EnglishText);

        updateBtn.addActionListener(updateStudentViewHandler);
        jPanel.add(updateBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    /**
     * 获取修改后的学生对象
     * @return
     */
    public  StudentDo buildUpdateStudent() {
        StudentDo studentDo=new StudentDo();
        studentDo.setId(Integer.parseInt(idText.getText()));
        studentDo.setName(nameText.getText());
        studentDo.setSex(sexText.getText());
        studentDo.setGrade(gradeText.getText());
        studentDo.setChinese(Integer.parseInt(ChineseText.getText()));   //!!!注意 JTextfield获取数据,需要用包装类转换
        studentDo.setMath(Integer.parseInt(MathText.getText()));
        studentDo.setEnglish(Integer.parseInt(EnglishText.getText()));
        return studentDo;
    }

}
